<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <!--      <el-form-item label="出库产品id" prop="commodityOutId">-->
      <!--        <el-input v-model="queryParams.commodityOutId" placeholder="请输入出库产品id" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <el-form-item label="产品名称" prop="commodityName">
        <el-input v-model="queryParams.commodityName" placeholder="请输入产品名称" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出品企业" prop="companyNameOut">
        <el-input v-model="queryParams.companyNameOut" placeholder="请输入出品企业" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品种类" prop="commodityType">
        <el-select v-model="queryParams.commodityType" placeholder="请选择产品种类" clearable>
          <el-option v-for="dict in dict.type.commodity_type" :key="dict.value" :label="dict.label"
                     :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品状态" prop="commodityStatus">
        <el-select v-model="queryParams.commodityStatus" placeholder="请选择产品状态" clearable>
          <el-option v-for="dict in dict.type.commodity_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产日期" prop="produceDate">
        <el-date-picker clearable v-model="queryParams.produceDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择生产日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="过期时间" prop="produceOverdate">
        <el-date-picker clearable v-model="queryParams.produceOverdate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择过期时间"
        >
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="采购公司id(用来进行多表联查)" prop="companyId">-->
      <!--        <el-input v-model="queryParams.companyId" placeholder="请输入采购公司id(用来进行多表联查)" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="产品采购数量" prop="commodityNum">-->
      <!--        <el-input v-model="queryParams.commodityNum" placeholder="请输入产品采购数量" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="产品唯一编码" prop="commodityUuid">-->
      <!--        <el-input v-model="queryParams.commodityUuid" placeholder="请输入产品唯一编码" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="采购员ID" prop="staffId">-->
      <!--        <el-input v-model="queryParams.staffId" placeholder="请输入采购员ID" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="采购员" prop="commodityStaff">-->
      <!--        <el-input v-model="queryParams.commodityStaff" placeholder="请输入采购员" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="采购时间" prop="shipTime">-->
      <!--        <el-date-picker clearable v-model="queryParams.shipTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择采购时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="审核员ID" prop="checkId">-->
      <!--        <el-input v-model="queryParams.checkId" placeholder="请输入审核员ID" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="审核员" prop="checkStaff">-->
      <!--        <el-input v-model="queryParams.checkStaff" placeholder="请输入审核员" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="审核时间" prop="checkTime">-->
      <!--        <el-date-picker clearable v-model="queryParams.checkTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择审核时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['trace:productInto:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['trace:productInto:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['trace:productInto:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['trace:productInto:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productIntoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品名称" align="center" prop="commodityName"/>
      <el-table-column label="出品企业" align="center" prop="companyNameOut"/>
      <!--      <el-table-column label="采购企业" align="center" prop="companyNameInto"/>-->
      <!--      <el-table-column label="采购数量" align="center" prop="commodityNum"/>-->
      <!--      <el-table-column label="产品规格" align="center" prop="commoditySize">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.commodity_size" :value="scope.row.commoditySize"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="产品种类" align="center" prop="commodityType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.commodity_type" :value="scope.row.commodityType"/>
        </template>
      </el-table-column>
      <el-table-column label="产品状态" align="center" prop="commodityStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.commodity_status" :value="scope.row.commodityStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="生产日期" align="center" prop="produceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.produceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center" prop="produceOverdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.produceOverdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['trace:productOut:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['trace:productOut:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>

      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <!--      <el-table-column label="产品采购id" align="center" prop="id" />-->
      <!--      <el-table-column label="出库产品id" align="center" prop="commodityOutId" />-->
      <!--      <el-table-column label="产品名称" align="center" prop="commodityName" />-->
      <!--      <el-table-column label="采购公司id(用来进行多表联查)" align="center" prop="companyId" />-->
      <!--      <el-table-column label="产品采购数量" align="center" prop="commodityNum" />-->
      <!--      <el-table-column label="产品唯一编码" align="center" prop="commodityUuid" />-->
      <!--      <el-table-column label="采购员ID" align="center" prop="staffId" />-->
      <!--      <el-table-column label="采购员" align="center" prop="commodityStaff" />-->
      <!--      <el-table-column label="采购时间" align="center" prop="shipTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.shipTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="审核员ID" align="center" prop="checkId" />-->
      <!--      <el-table-column label="审核员" align="center" prop="checkStaff" />-->
      <!--      <el-table-column label="审核时间" align="center" prop="checkTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <!--      <el-table-column label="防伪码" align="center" prop="secturityCode" />-->
      <!--      <el-table-column label="产品审核状态" align="center" prop="commodityStatus">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.commodity_status" :value="scope.row.commodityStatus" />-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['trace:productInto:edit']">修改-->
      <!--          </el-button>-->
      <!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['trace:productInto:remove']">删除-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />

    <!-- 添加或修改商品采购详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出库产品id" prop="commodityOutId">
          <el-input v-model="form.commodityOutId" placeholder="请输入出库产品id" @focus="nameFocus"/>
        </el-form-item>
        <el-form-item label="产品名称" prop="commodityName">
          <el-input v-model="form.commodityName" placeholder="请输入产品名称"/>
        </el-form-item>
        <el-form-item label="采购公司id(用来进行多表联查)" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入采购公司id(用来进行多表联查)" @focus="PurFocus"/>
        </el-form-item>
        <el-form-item label="产品采购数量" prop="commodityNum">
          <el-input v-model="form.commodityNum" placeholder="请输入产品采购数量"/>
        </el-form-item>
        <el-form-item label="产品唯一编码" prop="commodityUuid">
          <el-input v-model="form.commodityUuid" placeholder="请输入产品唯一编码"/>
        </el-form-item>
        <el-form-item label="采购员ID" prop="staffId">
          <el-input v-model="form.staffId" placeholder="请输入采购员ID"/>
        </el-form-item>
        <el-form-item label="采购员" prop="commodityStaff">
          <el-input v-model="form.commodityStaff" placeholder="请输入采购员"/>
        </el-form-item>
        <el-form-item label="采购时间" prop="shipTime">
          <el-date-picker clearable v-model="form.shipTime" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择采购时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核员ID" prop="checkId">
          <el-input v-model="form.checkId" placeholder="请输入审核员ID"/>
        </el-form-item>
        <el-form-item label="审核员" prop="checkStaff">
          <el-input v-model="form.checkStaff" placeholder="请输入审核员"/>
        </el-form-item>
        <el-form-item label="审核时间" prop="checkTime">
          <el-date-picker clearable v-model="form.checkTime" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择审核时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="防伪码" prop="secturityCode">
          <el-input v-model="form.secturityCode" placeholder="请输入防伪码"/>
        </el-form-item>
        <el-form-item label="产品审核状态" prop="commodityStatus">
          <el-select v-model="form.commodityStatus" placeholder="请选择产品审核状态">
            <el-option v-for="dict in dict.type.commodity_status" :key="dict.value" :label="dict.label"
                       :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- input出库产品获得焦点后触发 出库id-->
    <el-dialog title="采购企业" :visible.sync="inputDialog" width="70%" :before-close="inputDialogClose">
      <!-- 出库产品id -->
      <InputProductOut @uploadData="receiveData"></InputProductOut>
      <span slot="footer" class="dialog-footer">
        <el-button @click="inputDialog = false">取 消</el-button>
        <el-button type="primary" @click="inputDecideOn">确 定</el-button>
      </span>
    </el-dialog>
    <!-- input采购公司获得焦点后触发 出库id-->
    <el-dialog title="采购企业" :visible.sync="inputPurDialog" width="70%" :before-close="inputDialogClose">
      <!-- 出库产品id -->
      <InputPurchase @uploadPurData="receivePurData"></InputPurchase>
      <span slot="footer" class="dialog-footer">
        <el-button @click="inputPurDialog = false">取 消</el-button>
        <el-button type="primary" @click="inputPurDecideOn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProductInto,
  getProductInto,
  delProductInto,
  addProductInto,
  updateProductInto
} from '@/api/trace/productInto'
import InputProductOut from './inputProductOut.vue'
import InputPurchase from './inputPurchase.vue'

export default {
  name: 'ProductInto',
  dicts: ['commodity_type', 'commodity_status', 'commodity_size'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品采购详情表格数据
      productIntoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commodityOutId: null,
        commodityName: null,
        companyId: null,
        commodityNum: null,
        commodityUuid: null,
        staffId: null,
        commodityStaff: null,
        shipTime: null,
        checkId: null,
        checkStaff: null,
        checkTime: null,
        secturityCode: null,
        commodityStatus: null,
        companyNameInto: null,
        companyNameOut: null,
        remark: null,
        produceOverdate: null,
        produceDate: null
      },
      // 表单参数
      form: {
        commodityOutId: '',
        companyId: ''
      },
      // 表单校验
      rules: {
        id: [
          { required: true, message: '产品采购id不能为空', trigger: 'blur' }
        ],
        commodityOutId: [
          { required: true, message: '出库产品id不能为空', trigger: 'blur' }
        ],
        commodityName: [
          { required: true, message: '产品名称不能为空', trigger: 'blur' }
        ],
        companyId: [
          { required: true, message: '采购公司id(用来进行多表联查)不能为空', trigger: 'blur' }
        ],
        commodityNum: [
          { required: true, message: '产品采购数量不能为空', trigger: 'blur' }
        ],
        commodityUuid: [
          { required: true, message: '产品唯一编码不能为空', trigger: 'blur' }
        ],
        staffId: [
          { required: true, message: '采购员ID不能为空', trigger: 'blur' }
        ],
        commodityStaff: [
          { required: true, message: '采购员不能为空', trigger: 'blur' }
        ],
        shipTime: [
          { required: true, message: '采购时间不能为空', trigger: 'blur' }
        ],
        checkId: [
          { required: true, message: '审核员ID不能为空', trigger: 'blur' }
        ],
        checkStaff: [
          { required: true, message: '审核员不能为空', trigger: 'blur' }
        ],
        checkTime: [
          { required: true, message: '审核时间不能为空', trigger: 'blur' }
        ]
      },
      // 出库商品表单的显示隐藏
      inputDialog: false,
      // 子组件传来的值
      productOUt: {},
      // 采购公司获得焦点后触发
      inputPurDialog: false,
      // 采购企业子组件传来的数据
      producPurOut: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询商品采购详情列表 */
    getList() {
      this.loading = true
      listProductInto(this.queryParams).then(response => {
        console.log(response)
        this.productIntoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        commodityOutId: null,
        commodityName: null,
        companyId: null,
        commodityNum: null,
        commodityUuid: null,
        staffId: null,
        commodityStaff: null,
        shipTime: null,
        checkId: null,
        checkStaff: null,
        checkTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品采购详情'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getProductInto(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改商品采购详情'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductInto(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addProductInto(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除商品采购详情编号为"' + ids + '"的数据项？').then(function() {
        return delProductInto(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('trace/productInto/export', {
        ...this.queryParams
      }, `productInto_${new Date().getTime()}.xlsx`)
    },
    testHover() {
      alert('测试成功！！！')
    },
    // input点击关闭，弹出是否关闭按钮
    inputDialogClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    // 出库商品点击确定后触发
    inputDecideOn() {
      this.form.commodityOutId = this.productOUt.id
      this.inputDialog = false
    },
    // 控制出库产品的显示和隐藏
    nameFocus() {
      this.inputDialog = true
    },
    // 接收子组件传来的值
    receiveData(val) {
      this.productOUt = val
    },
    // 采购公司弹出框点击确定后触发
    inputPurDecideOn() {
      this.form.companyId = this.producPurOut.id
      this.inputPurDialog = false
    },
    // 采购公司输入框获得焦点后触发
    PurFocus() {
      this.inputPurDialog = true
    },
    receivePurData(val) {
      // console.log(val)
      this.producPurOut = val
    }
  },
  components: { InputProductOut, InputPurchase }
}
</script>
